package com.test.validation;

import com.opensymphony.xwork2.validator.ValidationException;
import com.opensymphony.xwork2.validator.validators.FieldValidatorSupport;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2018/9/12 21:56
 */
public class IDCardValidator extends FieldValidatorSupport {
    @Override
    public void validate(Object object) throws ValidationException {
        //1.获取字段的名字和值
        String fieldName = getFieldName();
        Object value = this.getFieldValue(fieldName, object);

        //2.验证
        IdcardValidate idcardValidate = new IdcardValidate();
        boolean result = idcardValidate.isValidatedAllIdcard((String) value);


        //3.若验证失败则
        if (!result) {
            addFieldError(fieldName, object);
        }
    }
}
